package cn.t222.config.mq;

import cn.t222.pojo.ItripHotelOrder;
import cn.t222.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class OrderCancelReciever {
    @Resource
    private OrderService orderService;

    @RabbitListener(queues = RabbitConfig.QUEUE_ORDER_CANCEL)
    public void receive(String orderNo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        System.out.println("消息接收时间：" + sdf.format(new Date()));
        System.out.println("执行取消订单的操作" + orderNo);
        //乐观锁取消订单
        ItripHotelOrder order = orderService.getByNo(orderNo);
        order.setOrderStatus(1);
        boolean success = orderService.update(order);
        if(!success) {
            //记录到数据库
            System.out.println("取消订单失败,数据已保存到异常订单信息表，请手动处理：" + orderNo +"," + sdf.format(new Date()));
        }
    }
}
